package com.xmler.plugin.adbrocket.utils;

import com.android.ddmlib.AndroidDebugBridge;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

public class ReflectHelper {

    public static int reflectAdbProcessOutput(final Process process, final ArrayList<String> errorOutput, final ArrayList<String> stdOutput, boolean waitForReaders, long timeout, TimeUnit unit) throws NoSuchMethodException {
        try {
            Class<AndroidDebugBridge> clazz = AndroidDebugBridge.class;
            Method method = clazz.getDeclaredMethod("grabProcessOutput", Process.class, ArrayList.class, ArrayList.class, boolean.class, long.class, TimeUnit.class); //, Long.class, TimeUnit.class
            if (method != null) {
                method.setAccessible(true);
                return (int) method.invoke(null, process, errorOutput, stdOutput, waitForReaders, timeout, unit); //
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return -1;
    }
}
